
CREATE TYPE ITEM_ID
	FROM INTEGER NULL
go



CREATE TYPE ITEM_NAME
	FROM NVARCHAR(50) NULL
go



CREATE TYPE ITEM_DESCRIPTION
	FROM NVARCHAR(200) NULL
go



CREATE TYPE ITEM_CREATE_DATE
	FROM DATETIME NULL
go



CREATE TYPE ITEM_UPDATE_DATE
	FROM DATETIME NULL
go



CREATE TYPE ITEM_STATUS_CODE
	FROM NVARCHAR(20) NULL
go



CREATE TYPE ITEM_CONTENT
	FROM NVARCHAR(500) NULL
go



CREATE TYPE ITEM_SEQUENCE
	FROM NUMERIC(3) NULL
go



CREATE TYPE PASSWORD
	FROM NVARCHAR(1024) NULL
go



CREATE TYPE USER_ID
	FROM NVARCHAR(20) NULL
go



CREATE TYPE MDL_CD
	FROM NVARCHAR(20) NULL
go



CREATE TYPE SALT_ALGORITHM
	FROM NVARCHAR(100) NULL
go



CREATE TYPE STATUS_CODE
	FROM INTEGER NULL
go



CREATE TYPE Adress
	FROM NVARCHAR(200) NULL
go



CREATE TYPE Phone
	FROM VARCHAR(20) NULL
go



CREATE TYPE ZIP_CODE
	FROM NVARCHAR(20) NULL
go



CREATE TYPE COUNTRY
	FROM CHAR(3) NULL
go



CREATE TYPE PHONE_TYPE
	FROM NVARCHAR(20) NULL
go



CREATE TYPE PHONE_NUMBER
	FROM NVARCHAR(20) NULL
go



CREATE TABLE TsAddress
( 
	AddressId            ITEM_ID IDENTITY ( 1,1 ) ,
	UserId               USER_ID  NOT NULL ,
	CountryCode          ITEM_STATUS_CODE ,
	ZipCode              ZIP_CODE ,
	Address              Adress ,
	CreateUserId         USER_ID ,
	CreateDate           ITEM_CREATE_DATE ,
	UpdateUserId         USER_ID ,
	UpdateDate           ITEM_UPDATE_DATE 
)
go



ALTER TABLE TsAddress
	ADD CONSTRAINT XPKTs_Address PRIMARY KEY  CLUSTERED (AddressId ASC,UserId ASC)
go



CREATE TABLE TsArticle
( 
	ArticleId            ITEM_ID IDENTITY ( 1,1 ) ,
	CategoryId           ITEM_ID  NOT NULL ,
	ArticleType          ITEM_STATUS_CODE ,
	ArticleTitle         ITEM_NAME ,
	RepresentativeImage  ITEM_CONTENT ,
	ArticleIntroduction  ITEM_CONTENT ,
	FocusId              ITEM_ID ,
	StatusCode           STATUS_CODE ,
	ApproveUserId        USER_ID ,
	ApproveDate          ITEM_CREATE_DATE ,
	HotNewFlag           bit  NULL ,
	CreateUserId         USER_ID ,
	CreateDate           ITEM_CREATE_DATE ,
	UpdateUserId         USER_ID ,
	UpdateDate           ITEM_UPDATE_DATE 
)
go



ALTER TABLE TsArticle
	ADD CONSTRAINT XPKTs_Article PRIMARY KEY  CLUSTERED (ArticleId ASC,CategoryId ASC)
go



CREATE TABLE TsArticleDetail
( 
	SequenceId           ITEM_SEQUENCE  NOT NULL ,
	ArticleId            ITEM_ID  NOT NULL ,
	CategoryId           ITEM_ID  NOT NULL ,
	DetailContent        ITEM_CONTENT 
)
go



ALTER TABLE TsArticleDetail
	ADD CONSTRAINT XPKTs_Article_Detail PRIMARY KEY  CLUSTERED (SequenceId ASC,ArticleId ASC,CategoryId ASC)
go



CREATE TABLE TsCommonCode
( 
	CodeId               ITEM_ID IDENTITY ( 1,1 ) ,
	Code                 ITEM_ID ,
	CodeName             ITEM_NAME ,
	CodeDescription      ITEM_DESCRIPTION ,
	ParentCode           ITEM_ID ,
	CreateUserId         USER_ID ,
	CreateDate           ITEM_CREATE_DATE ,
	UpdateUserId         USER_ID ,
	UpdateDate           ITEM_UPDATE_DATE 
)
go



ALTER TABLE TsCommonCode
	ADD CONSTRAINT XPKTs_Common_Code PRIMARY KEY  CLUSTERED (CodeId ASC)
go



CREATE TABLE TsFocus
( 
	FocusId              ITEM_ID IDENTITY ( 1,1 ) ,
	CategoryId           ITEM_ID  NOT NULL ,
	FocusName            ITEM_NAME ,
	FocusDescription     ITEM_DESCRIPTION ,
	CreateUserId         USER_ID ,
	CreateDate           ITEM_CREATE_DATE ,
	UpdateUserId         USER_ID ,
	UpdateDate           ITEM_UPDATE_DATE 
)
go



ALTER TABLE TsFocus
	ADD CONSTRAINT XPKTs_Focus PRIMARY KEY  CLUSTERED (FocusId ASC,CategoryId ASC)
go



CREATE TABLE TsMenu
( 
	MenuId               ITEM_ID IDENTITY ( 1,1 ) ,
	MenuName             ITEM_NAME ,
	MenuUrl              ITEM_NAME ,
	ModuleId             MDL_CD ,
	ModuleDetailId       ITEM_ID ,
	MenuDescription      ITEM_DESCRIPTION ,
	MessageCode          ITEM_STATUS_CODE ,
	OrderNumber          ITEM_SEQUENCE ,
	ParentMenuId         ITEM_ID ,
	CreateUserId         USER_ID ,
	CreateDate           ITEM_CREATE_DATE ,
	UpdateUserId         USER_ID ,
	UpdateDate           ITEM_UPDATE_DATE 
)
go



ALTER TABLE TsMenu
	ADD CONSTRAINT XPKTs_Menu PRIMARY KEY  CLUSTERED (MenuId ASC)
go



CREATE TABLE TsMessage
( 
	MessageId            ITEM_ID IDENTITY ( 1,1 ) ,
	LanguageType         ITEM_STATUS_CODE ,
	MessageType          ITEM_STATUS_CODE ,
	MessageCode          ITEM_STATUS_CODE ,
	MessageContent       ITEM_NAME ,
	CreateUserId         USER_ID ,
	CreateDate           ITEM_CREATE_DATE ,
	UpdateUserId         USER_ID ,
	UpdateDate           ITEM_UPDATE_DATE 
)
go



ALTER TABLE TsMessage
	ADD CONSTRAINT XPKTs_Message PRIMARY KEY  CLUSTERED (MessageId ASC)
go



CREATE TABLE TsModule
( 
	ModuleId             MDL_CD  NOT NULL ,
	ModuleName           ITEM_NAME ,
	CreateUserId         USER_ID ,
	CreateDate           ITEM_CREATE_DATE ,
	UpdateUserId         USER_ID ,
	UpdateDate           ITEM_UPDATE_DATE 
)
go



ALTER TABLE TsModule
	ADD CONSTRAINT XPKTs_Module PRIMARY KEY  CLUSTERED (ModuleId ASC)
go



CREATE TABLE TsModuleRight
( 
	ModuleRightId        ITEM_ID IDENTITY ( 1,1 ) ,
	ModuleId             MDL_CD  NOT NULL ,
	RightId              ITEM_ID  NOT NULL ,
	CreateUserId         USER_ID ,
	CreateDate           ITEM_CREATE_DATE ,
	UpdateUserId         USER_ID ,
	UpdateDate           ITEM_UPDATE_DATE 
)
go



ALTER TABLE TsModuleRight
	ADD CONSTRAINT XPKTs_Module_Right PRIMARY KEY  CLUSTERED (ModuleRightId ASC,ModuleId ASC,RightId ASC)
go



CREATE TABLE TsNewsCategory
( 
	CategoryId           ITEM_ID IDENTITY ( 1,1 ) ,
	CategoryName         ITEM_NAME ,
	CategoryDescription  ITEM_DESCRIPTION ,
	ParentCategoryId     ITEM_ID ,
	StatusCode           STATUS_CODE ,
	CreateUserId         USER_ID ,
	CreateDate           ITEM_CREATE_DATE ,
	UpdateUserId         USER_ID ,
	UpdateDate           ITEM_UPDATE_DATE 
)
go



ALTER TABLE TsNewsCategory
	ADD CONSTRAINT XPKTs_News_Category PRIMARY KEY  CLUSTERED (CategoryId ASC)
go



CREATE TABLE TsPermission
( 
	PermissionId         ITEM_ID IDENTITY ( 1,1 ) ,
	UserId               USER_ID  NOT NULL ,
	RoleId               ITEM_ID  NOT NULL ,
	CreateUserId         USER_ID ,
	CreateDate           ITEM_CREATE_DATE ,
	UpdateUserId         USER_ID ,
	UpdateDate           ITEM_UPDATE_DATE 
)
go



ALTER TABLE TsPermission
	ADD CONSTRAINT XPKTs_Permission PRIMARY KEY  CLUSTERED (PermissionId ASC,UserId ASC,RoleId ASC)
go



CREATE TABLE TsPhone
( 
	PhoneId              ITEM_ID IDENTITY ( 1,1 ) ,
	UserId               USER_ID  NOT NULL ,
	PhoneType            PHONE_TYPE ,
	PhoneNumber          PHONE_NUMBER ,
	CreateUserId         USER_ID ,
	CreateDate           ITEM_CREATE_DATE ,
	UpdateUserId         USER_ID ,
	UpdateDate           ITEM_UPDATE_DATE 
)
go



ALTER TABLE TsPhone
	ADD CONSTRAINT XPKTs_Phone PRIMARY KEY  CLUSTERED (PhoneId ASC,UserId ASC)
go



CREATE TABLE TsRight
( 
	RightId              ITEM_ID IDENTITY ( 1,1 ) ,
	RightName            ITEM_NAME ,
	CreateUserId         USER_ID ,
	CreateDate           ITEM_CREATE_DATE ,
	UpdateUserId         USER_ID ,
	UpdateDate           ITEM_UPDATE_DATE 
)
go



ALTER TABLE TsRight
	ADD CONSTRAINT XPKTs_Right PRIMARY KEY  CLUSTERED (RightId ASC)
go



CREATE TABLE TsRole
( 
	RoleId               ITEM_ID IDENTITY ( 1,1 ) ,
	RoleName             ITEM_NAME ,
	CreateUserId         USER_ID ,
	CreateDate           ITEM_CREATE_DATE ,
	UpdateUserId         USER_ID ,
	UpdateDate           ITEM_UPDATE_DATE 
)
go



ALTER TABLE TsRole
	ADD CONSTRAINT XPKTs_Role PRIMARY KEY  CLUSTERED (RoleId ASC)
go



CREATE TABLE TsRoleRegistry
( 
	RoleRegistryId       ITEM_ID IDENTITY ( 1,1 ) ,
	ModuleId             MDL_CD  NOT NULL ,
	RoleId               ITEM_ID  NOT NULL ,
	Rght                 ITEM_ID ,
	CreateUserId         USER_ID ,
	CreateDate           ITEM_CREATE_DATE ,
	UpdateUserId         USER_ID ,
	UpdateDate           ITEM_UPDATE_DATE 
)
go



ALTER TABLE TsRoleRegistry
	ADD CONSTRAINT XPKTs_Role_Registry PRIMARY KEY  CLUSTERED (RoleRegistryId ASC,ModuleId ASC,RoleId ASC)
go



CREATE TABLE TsUser
( 
	UserId               USER_ID  NOT NULL ,
	Salt                 SALT_ALGORITHM ,
	Password             PASSWORD ,
	UserName             ITEM_NAME ,
	DateOfBirth          datetime  NULL ,
	Sex                  nvarchar(1)  NULL ,
	KeyVerify            PASSWORD ,
	KeyVerifyExpire      ITEM_CREATE_DATE ,
	RegisterType         ITEM_STATUS_CODE ,
	Email                ITEM_NAME ,
	ActiveCode           ITEM_STATUS_CODE ,
	ActiveDate           ITEM_CREATE_DATE ,
	LastLoginDate        ITEM_CREATE_DATE ,
	DeleteFlag           bit  NULL ,
	CreateUserId         USER_ID ,
	CreateDate           ITEM_CREATE_DATE ,
	UpdateUserId         USER_ID ,
	UpdateDate           ITEM_UPDATE_DATE ,
	Reason               ITEM_DESCRIPTION 
)
go



ALTER TABLE TsUser
	ADD CONSTRAINT XPKTs_User PRIMARY KEY  CLUSTERED (UserId ASC)
go




ALTER TABLE TsAddress
	ADD CONSTRAINT R_25 FOREIGN KEY (UserId) REFERENCES TsUser(UserId)
		ON DELETE NO ACTION
		ON UPDATE NO ACTION
go




ALTER TABLE TsArticle
	ADD CONSTRAINT R_1 FOREIGN KEY (CategoryId) REFERENCES TsNewsCategory(CategoryId)
		ON DELETE NO ACTION
		ON UPDATE NO ACTION
go




ALTER TABLE TsArticle
	ADD CONSTRAINT R_8 FOREIGN KEY (FocusId,CategoryId) REFERENCES TsFocus(FocusId,CategoryId)
		ON DELETE NO ACTION
		ON UPDATE NO ACTION
go




ALTER TABLE TsArticleDetail
	ADD CONSTRAINT R_21 FOREIGN KEY (ArticleId,CategoryId) REFERENCES TsArticle(ArticleId,CategoryId)
		ON DELETE NO ACTION
		ON UPDATE NO ACTION
go




ALTER TABLE TsFocus
	ADD CONSTRAINT R_3 FOREIGN KEY (CategoryId) REFERENCES TsNewsCategory(CategoryId)
		ON DELETE NO ACTION
		ON UPDATE NO ACTION
go




ALTER TABLE TsMenu
	ADD CONSTRAINT R_18 FOREIGN KEY (ModuleId) REFERENCES TsModule(ModuleId)
		ON DELETE NO ACTION
		ON UPDATE NO ACTION
go




ALTER TABLE TsModuleRight
	ADD CONSTRAINT R_13 FOREIGN KEY (ModuleId) REFERENCES TsModule(ModuleId)
		ON DELETE NO ACTION
		ON UPDATE NO ACTION
go




ALTER TABLE TsModuleRight
	ADD CONSTRAINT R_14 FOREIGN KEY (RightId) REFERENCES TsRight(RightId)
		ON DELETE NO ACTION
		ON UPDATE NO ACTION
go




ALTER TABLE TsPermission
	ADD CONSTRAINT R_11 FOREIGN KEY (RoleId) REFERENCES TsRole(RoleId)
		ON DELETE NO ACTION
		ON UPDATE NO ACTION
go




ALTER TABLE TsPermission
	ADD CONSTRAINT R_12 FOREIGN KEY (UserId) REFERENCES TsUser(UserId)
		ON DELETE NO ACTION
		ON UPDATE NO ACTION
go




ALTER TABLE TsPhone
	ADD CONSTRAINT R_26 FOREIGN KEY (UserId) REFERENCES TsUser(UserId)
		ON DELETE NO ACTION
		ON UPDATE NO ACTION
go




ALTER TABLE TsRoleRegistry
	ADD CONSTRAINT R_15 FOREIGN KEY (ModuleId) REFERENCES TsModule(ModuleId)
		ON DELETE NO ACTION
		ON UPDATE NO ACTION
go




ALTER TABLE TsRoleRegistry
	ADD CONSTRAINT R_16 FOREIGN KEY (RoleId) REFERENCES TsRole(RoleId)
		ON DELETE NO ACTION
		ON UPDATE NO ACTION
go




CREATE TRIGGER tD_TsAddress ON TsAddress FOR DELETE AS
/* ERwin Builtin Trigger */
/* DELETE trigger on TsAddress */
BEGIN
  DECLARE  @errno   int,
           @errmsg  varchar(255)
    /* ERwin Builtin Trigger */
    /* TsUser  TsAddress on child delete no action */
    /* ERWIN_RELATION:CHECKSUM="00011957", PARENT_OWNER="", PARENT_TABLE="TsUser"
    CHILD_OWNER="", CHILD_TABLE="TsAddress"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_25", FK_COLUMNS="UserId" */
    IF EXISTS (SELECT * FROM deleted,TsUser
      WHERE
        /* %JoinFKPK(deleted,TsUser," = "," AND") */
        deleted.UserId = TsUser.UserId AND
        NOT EXISTS (
          SELECT * FROM TsAddress
          WHERE
            /* %JoinFKPK(TsAddress,TsUser," = "," AND") */
            TsAddress.UserId = TsUser.UserId
        )
    )
    BEGIN
      SELECT @errno  = 30010,
             @errmsg = 'Cannot delete last TsAddress because TsUser exists.'
      GOTO ERROR
    END


    /* ERwin Builtin Trigger */
    RETURN
ERROR:
    raiserror @errno @errmsg
    rollback transaction
END

go


CREATE TRIGGER tU_TsAddress ON TsAddress FOR UPDATE AS
/* ERwin Builtin Trigger */
/* UPDATE trigger on TsAddress */
BEGIN
  DECLARE  @NUMROWS int,
           @nullcnt int,
           @validcnt int,
           @insAddressId ITEM_ID, 
           @insUserId USER_ID,
           @errno   int,
           @errmsg  varchar(255)

  SELECT @NUMROWS = @@rowcount
  /* ERwin Builtin Trigger */
  /* TsUser  TsAddress on child update no action */
  /* ERWIN_RELATION:CHECKSUM="00013c21", PARENT_OWNER="", PARENT_TABLE="TsUser"
    CHILD_OWNER="", CHILD_TABLE="TsAddress"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_25", FK_COLUMNS="UserId" */
  IF
    /* %ChildFK(" OR",UPDATE) */
    UPDATE(UserId)
  BEGIN
    SELECT @nullcnt = 0
    SELECT @validcnt = count(*)
      FROM inserted,TsUser
        WHERE
          /* %JoinFKPK(inserted,TsUser) */
          inserted.UserId = TsUser.UserId
    /* %NotnullFK(inserted," IS NULL","select @nullcnt = count(*) from inserted where"," AND") */
    
    IF @validcnt + @nullcnt != @NUMROWS
    BEGIN
      SELECT @errno  = 30007,
             @errmsg = 'Cannot update TsAddress because TsUser does not exist.'
      GOTO ERROR
    END
  END


  /* ERwin Builtin Trigger */
  RETURN
ERROR:
    raiserror @errno @errmsg
    rollback transaction
END

go




CREATE TRIGGER tD_TsArticle ON TsArticle FOR DELETE AS
/* ERwin Builtin Trigger */
/* DELETE trigger on TsArticle */
BEGIN
  DECLARE  @errno   int,
           @errmsg  varchar(255)
    /* ERwin Builtin Trigger */
    /* TsArticle  TsArticleDetail on parent delete no action */
    /* ERWIN_RELATION:CHECKSUM="0003a636", PARENT_OWNER="", PARENT_TABLE="TsArticle"
    CHILD_OWNER="", CHILD_TABLE="TsArticleDetail"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_21", FK_COLUMNS="ArticleId""CategoryId" */
    IF EXISTS (
      SELECT * FROM deleted,TsArticleDetail
      WHERE
        /*  %JoinFKPK(TsArticleDetail,deleted," = "," AND") */
        TsArticleDetail.ArticleId = deleted.ArticleId AND
        TsArticleDetail.CategoryId = deleted.CategoryId
    )
    BEGIN
      SELECT @errno  = 30001,
             @errmsg = 'Cannot delete TsArticle because TsArticleDetail exists.'
      GOTO ERROR
    END

    /* ERwin Builtin Trigger */
    /* TsNewsCategory  TsArticle on child delete no action */
    /* ERWIN_RELATION:CHECKSUM="00000000", PARENT_OWNER="", PARENT_TABLE="TsNewsCategory"
    CHILD_OWNER="", CHILD_TABLE="TsArticle"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_1", FK_COLUMNS="CategoryId" */
    IF EXISTS (SELECT * FROM deleted,TsNewsCategory
      WHERE
        /* %JoinFKPK(deleted,TsNewsCategory," = "," AND") */
        deleted.CategoryId = TsNewsCategory.CategoryId AND
        NOT EXISTS (
          SELECT * FROM TsArticle
          WHERE
            /* %JoinFKPK(TsArticle,TsNewsCategory," = "," AND") */
            TsArticle.CategoryId = TsNewsCategory.CategoryId
        )
    )
    BEGIN
      SELECT @errno  = 30010,
             @errmsg = 'Cannot delete last TsArticle because TsNewsCategory exists.'
      GOTO ERROR
    END

    /* ERwin Builtin Trigger */
    /* TsFocus  TsArticle on child delete no action */
    /* ERWIN_RELATION:CHECKSUM="00000000", PARENT_OWNER="", PARENT_TABLE="TsFocus"
    CHILD_OWNER="", CHILD_TABLE="TsArticle"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_8", FK_COLUMNS="FocusId""CategoryId" */
    IF EXISTS (SELECT * FROM deleted,TsFocus
      WHERE
        /* %JoinFKPK(deleted,TsFocus," = "," AND") */
        deleted.FocusId = TsFocus.FocusId AND
        deleted.CategoryId = TsFocus.CategoryId AND
        NOT EXISTS (
          SELECT * FROM TsArticle
          WHERE
            /* %JoinFKPK(TsArticle,TsFocus," = "," AND") */
            TsArticle.FocusId = TsFocus.FocusId AND
            TsArticle.CategoryId = TsFocus.CategoryId
        )
    )
    BEGIN
      SELECT @errno  = 30010,
             @errmsg = 'Cannot delete last TsArticle because TsFocus exists.'
      GOTO ERROR
    END


    /* ERwin Builtin Trigger */
    RETURN
ERROR:
    raiserror @errno @errmsg
    rollback transaction
END

go


CREATE TRIGGER tU_TsArticle ON TsArticle FOR UPDATE AS
/* ERwin Builtin Trigger */
/* UPDATE trigger on TsArticle */
BEGIN
  DECLARE  @NUMROWS int,
           @nullcnt int,
           @validcnt int,
           @insArticleId ITEM_ID, 
           @insCategoryId ITEM_ID,
           @errno   int,
           @errmsg  varchar(255)

  SELECT @NUMROWS = @@rowcount
  /* ERwin Builtin Trigger */
  /* TsArticle  TsArticleDetail on parent update no action */
  /* ERWIN_RELATION:CHECKSUM="000427c7", PARENT_OWNER="", PARENT_TABLE="TsArticle"
    CHILD_OWNER="", CHILD_TABLE="TsArticleDetail"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_21", FK_COLUMNS="ArticleId""CategoryId" */
  IF
    /* %ParentPK(" OR",UPDATE) */
    UPDATE(ArticleId) OR
    UPDATE(CategoryId)
  BEGIN
    IF EXISTS (
      SELECT * FROM deleted,TsArticleDetail
      WHERE
        /*  %JoinFKPK(TsArticleDetail,deleted," = "," AND") */
        TsArticleDetail.ArticleId = deleted.ArticleId AND
        TsArticleDetail.CategoryId = deleted.CategoryId
    )
    BEGIN
      SELECT @errno  = 30005,
             @errmsg = 'Cannot update TsArticle because TsArticleDetail exists.'
      GOTO ERROR
    END
  END

  /* ERwin Builtin Trigger */
  /* TsNewsCategory  TsArticle on child update no action */
  /* ERWIN_RELATION:CHECKSUM="00000000", PARENT_OWNER="", PARENT_TABLE="TsNewsCategory"
    CHILD_OWNER="", CHILD_TABLE="TsArticle"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_1", FK_COLUMNS="CategoryId" */
  IF
    /* %ChildFK(" OR",UPDATE) */
    UPDATE(CategoryId)
  BEGIN
    SELECT @nullcnt = 0
    SELECT @validcnt = count(*)
      FROM inserted,TsNewsCategory
        WHERE
          /* %JoinFKPK(inserted,TsNewsCategory) */
          inserted.CategoryId = TsNewsCategory.CategoryId
    /* %NotnullFK(inserted," IS NULL","select @nullcnt = count(*) from inserted where"," AND") */
    
    IF @validcnt + @nullcnt != @NUMROWS
    BEGIN
      SELECT @errno  = 30007,
             @errmsg = 'Cannot update TsArticle because TsNewsCategory does not exist.'
      GOTO ERROR
    END
  END

  /* ERwin Builtin Trigger */
  /* TsFocus  TsArticle on child update no action */
  /* ERWIN_RELATION:CHECKSUM="00000000", PARENT_OWNER="", PARENT_TABLE="TsFocus"
    CHILD_OWNER="", CHILD_TABLE="TsArticle"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_8", FK_COLUMNS="FocusId""CategoryId" */
  IF
    /* %ChildFK(" OR",UPDATE) */
    UPDATE(FocusId) OR
    UPDATE(CategoryId)
  BEGIN
    SELECT @nullcnt = 0
    SELECT @validcnt = count(*)
      FROM inserted,TsFocus
        WHERE
          /* %JoinFKPK(inserted,TsFocus) */
          inserted.FocusId = TsFocus.FocusId and
          inserted.CategoryId = TsFocus.CategoryId
    /* %NotnullFK(inserted," IS NULL","select @nullcnt = count(*) from inserted where"," AND") */
    select @nullcnt = count(*) from inserted where
      inserted.FocusId IS NULL AND
      inserted.CategoryId IS NULL
    IF @validcnt + @nullcnt != @NUMROWS
    BEGIN
      SELECT @errno  = 30007,
             @errmsg = 'Cannot update TsArticle because TsFocus does not exist.'
      GOTO ERROR
    END
  END


  /* ERwin Builtin Trigger */
  RETURN
ERROR:
    raiserror @errno @errmsg
    rollback transaction
END

go




CREATE TRIGGER tD_TsArticleDetail ON TsArticleDetail FOR DELETE AS
/* ERwin Builtin Trigger */
/* DELETE trigger on TsArticleDetail */
BEGIN
  DECLARE  @errno   int,
           @errmsg  varchar(255)
    /* ERwin Builtin Trigger */
    /* TsArticle  TsArticleDetail on child delete no action */
    /* ERWIN_RELATION:CHECKSUM="000162c9", PARENT_OWNER="", PARENT_TABLE="TsArticle"
    CHILD_OWNER="", CHILD_TABLE="TsArticleDetail"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_21", FK_COLUMNS="ArticleId""CategoryId" */
    IF EXISTS (SELECT * FROM deleted,TsArticle
      WHERE
        /* %JoinFKPK(deleted,TsArticle," = "," AND") */
        deleted.ArticleId = TsArticle.ArticleId AND
        deleted.CategoryId = TsArticle.CategoryId AND
        NOT EXISTS (
          SELECT * FROM TsArticleDetail
          WHERE
            /* %JoinFKPK(TsArticleDetail,TsArticle," = "," AND") */
            TsArticleDetail.ArticleId = TsArticle.ArticleId AND
            TsArticleDetail.CategoryId = TsArticle.CategoryId
        )
    )
    BEGIN
      SELECT @errno  = 30010,
             @errmsg = 'Cannot delete last TsArticleDetail because TsArticle exists.'
      GOTO ERROR
    END


    /* ERwin Builtin Trigger */
    RETURN
ERROR:
    raiserror @errno @errmsg
    rollback transaction
END

go


CREATE TRIGGER tU_TsArticleDetail ON TsArticleDetail FOR UPDATE AS
/* ERwin Builtin Trigger */
/* UPDATE trigger on TsArticleDetail */
BEGIN
  DECLARE  @NUMROWS int,
           @nullcnt int,
           @validcnt int,
           @insSequenceId ITEM_SEQUENCE, 
           @insArticleId ITEM_ID, 
           @insCategoryId ITEM_ID,
           @errno   int,
           @errmsg  varchar(255)

  SELECT @NUMROWS = @@rowcount
  /* ERwin Builtin Trigger */
  /* TsArticle  TsArticleDetail on child update no action */
  /* ERWIN_RELATION:CHECKSUM="000176bf", PARENT_OWNER="", PARENT_TABLE="TsArticle"
    CHILD_OWNER="", CHILD_TABLE="TsArticleDetail"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_21", FK_COLUMNS="ArticleId""CategoryId" */
  IF
    /* %ChildFK(" OR",UPDATE) */
    UPDATE(ArticleId) OR
    UPDATE(CategoryId)
  BEGIN
    SELECT @nullcnt = 0
    SELECT @validcnt = count(*)
      FROM inserted,TsArticle
        WHERE
          /* %JoinFKPK(inserted,TsArticle) */
          inserted.ArticleId = TsArticle.ArticleId and
          inserted.CategoryId = TsArticle.CategoryId
    /* %NotnullFK(inserted," IS NULL","select @nullcnt = count(*) from inserted where"," AND") */
    
    IF @validcnt + @nullcnt != @NUMROWS
    BEGIN
      SELECT @errno  = 30007,
             @errmsg = 'Cannot update TsArticleDetail because TsArticle does not exist.'
      GOTO ERROR
    END
  END


  /* ERwin Builtin Trigger */
  RETURN
ERROR:
    raiserror @errno @errmsg
    rollback transaction
END

go




CREATE TRIGGER tD_TsFocus ON TsFocus FOR DELETE AS
/* ERwin Builtin Trigger */
/* DELETE trigger on TsFocus */
BEGIN
  DECLARE  @errno   int,
           @errmsg  varchar(255)
    /* ERwin Builtin Trigger */
    /* TsFocus  TsArticle on parent delete no action */
    /* ERWIN_RELATION:CHECKSUM="00024b1c", PARENT_OWNER="", PARENT_TABLE="TsFocus"
    CHILD_OWNER="", CHILD_TABLE="TsArticle"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_8", FK_COLUMNS="FocusId""CategoryId" */
    IF EXISTS (
      SELECT * FROM deleted,TsArticle
      WHERE
        /*  %JoinFKPK(TsArticle,deleted," = "," AND") */
        TsArticle.FocusId = deleted.FocusId AND
        TsArticle.CategoryId = deleted.CategoryId
    )
    BEGIN
      SELECT @errno  = 30001,
             @errmsg = 'Cannot delete TsFocus because TsArticle exists.'
      GOTO ERROR
    END

    /* ERwin Builtin Trigger */
    /* TsNewsCategory  TsFocus on child delete no action */
    /* ERWIN_RELATION:CHECKSUM="00000000", PARENT_OWNER="", PARENT_TABLE="TsNewsCategory"
    CHILD_OWNER="", CHILD_TABLE="TsFocus"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_3", FK_COLUMNS="CategoryId" */
    IF EXISTS (SELECT * FROM deleted,TsNewsCategory
      WHERE
        /* %JoinFKPK(deleted,TsNewsCategory," = "," AND") */
        deleted.CategoryId = TsNewsCategory.CategoryId AND
        NOT EXISTS (
          SELECT * FROM TsFocus
          WHERE
            /* %JoinFKPK(TsFocus,TsNewsCategory," = "," AND") */
            TsFocus.CategoryId = TsNewsCategory.CategoryId
        )
    )
    BEGIN
      SELECT @errno  = 30010,
             @errmsg = 'Cannot delete last TsFocus because TsNewsCategory exists.'
      GOTO ERROR
    END


    /* ERwin Builtin Trigger */
    RETURN
ERROR:
    raiserror @errno @errmsg
    rollback transaction
END

go


CREATE TRIGGER tU_TsFocus ON TsFocus FOR UPDATE AS
/* ERwin Builtin Trigger */
/* UPDATE trigger on TsFocus */
BEGIN
  DECLARE  @NUMROWS int,
           @nullcnt int,
           @validcnt int,
           @insFocusId ITEM_ID, 
           @insCategoryId ITEM_ID,
           @errno   int,
           @errmsg  varchar(255)

  SELECT @NUMROWS = @@rowcount
  /* ERwin Builtin Trigger */
  /* TsFocus  TsArticle on parent update no action */
  /* ERWIN_RELATION:CHECKSUM="00028bc9", PARENT_OWNER="", PARENT_TABLE="TsFocus"
    CHILD_OWNER="", CHILD_TABLE="TsArticle"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_8", FK_COLUMNS="FocusId""CategoryId" */
  IF
    /* %ParentPK(" OR",UPDATE) */
    UPDATE(FocusId) OR
    UPDATE(CategoryId)
  BEGIN
    IF EXISTS (
      SELECT * FROM deleted,TsArticle
      WHERE
        /*  %JoinFKPK(TsArticle,deleted," = "," AND") */
        TsArticle.FocusId = deleted.FocusId AND
        TsArticle.CategoryId = deleted.CategoryId
    )
    BEGIN
      SELECT @errno  = 30005,
             @errmsg = 'Cannot update TsFocus because TsArticle exists.'
      GOTO ERROR
    END
  END

  /* ERwin Builtin Trigger */
  /* TsNewsCategory  TsFocus on child update no action */
  /* ERWIN_RELATION:CHECKSUM="00000000", PARENT_OWNER="", PARENT_TABLE="TsNewsCategory"
    CHILD_OWNER="", CHILD_TABLE="TsFocus"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_3", FK_COLUMNS="CategoryId" */
  IF
    /* %ChildFK(" OR",UPDATE) */
    UPDATE(CategoryId)
  BEGIN
    SELECT @nullcnt = 0
    SELECT @validcnt = count(*)
      FROM inserted,TsNewsCategory
        WHERE
          /* %JoinFKPK(inserted,TsNewsCategory) */
          inserted.CategoryId = TsNewsCategory.CategoryId
    /* %NotnullFK(inserted," IS NULL","select @nullcnt = count(*) from inserted where"," AND") */
    
    IF @validcnt + @nullcnt != @NUMROWS
    BEGIN
      SELECT @errno  = 30007,
             @errmsg = 'Cannot update TsFocus because TsNewsCategory does not exist.'
      GOTO ERROR
    END
  END


  /* ERwin Builtin Trigger */
  RETURN
ERROR:
    raiserror @errno @errmsg
    rollback transaction
END

go




CREATE TRIGGER tD_TsMenu ON TsMenu FOR DELETE AS
/* ERwin Builtin Trigger */
/* DELETE trigger on TsMenu */
BEGIN
  DECLARE  @errno   int,
           @errmsg  varchar(255)
    /* ERwin Builtin Trigger */
    /* TsModule  TsMenu on child delete no action */
    /* ERWIN_RELATION:CHECKSUM="000129be", PARENT_OWNER="", PARENT_TABLE="TsModule"
    CHILD_OWNER="", CHILD_TABLE="TsMenu"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_18", FK_COLUMNS="ModuleId" */
    IF EXISTS (SELECT * FROM deleted,TsModule
      WHERE
        /* %JoinFKPK(deleted,TsModule," = "," AND") */
        deleted.ModuleId = TsModule.ModuleId AND
        NOT EXISTS (
          SELECT * FROM TsMenu
          WHERE
            /* %JoinFKPK(TsMenu,TsModule," = "," AND") */
            TsMenu.ModuleId = TsModule.ModuleId
        )
    )
    BEGIN
      SELECT @errno  = 30010,
             @errmsg = 'Cannot delete last TsMenu because TsModule exists.'
      GOTO ERROR
    END


    /* ERwin Builtin Trigger */
    RETURN
ERROR:
    raiserror @errno @errmsg
    rollback transaction
END

go


CREATE TRIGGER tU_TsMenu ON TsMenu FOR UPDATE AS
/* ERwin Builtin Trigger */
/* UPDATE trigger on TsMenu */
BEGIN
  DECLARE  @NUMROWS int,
           @nullcnt int,
           @validcnt int,
           @insMenuId ITEM_ID,
           @errno   int,
           @errmsg  varchar(255)

  SELECT @NUMROWS = @@rowcount
  /* ERwin Builtin Trigger */
  /* TsModule  TsMenu on child update no action */
  /* ERWIN_RELATION:CHECKSUM="00016c12", PARENT_OWNER="", PARENT_TABLE="TsModule"
    CHILD_OWNER="", CHILD_TABLE="TsMenu"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_18", FK_COLUMNS="ModuleId" */
  IF
    /* %ChildFK(" OR",UPDATE) */
    UPDATE(ModuleId)
  BEGIN
    SELECT @nullcnt = 0
    SELECT @validcnt = count(*)
      FROM inserted,TsModule
        WHERE
          /* %JoinFKPK(inserted,TsModule) */
          inserted.ModuleId = TsModule.ModuleId
    /* %NotnullFK(inserted," IS NULL","select @nullcnt = count(*) from inserted where"," AND") */
    select @nullcnt = count(*) from inserted where
      inserted.ModuleId IS NULL
    IF @validcnt + @nullcnt != @NUMROWS
    BEGIN
      SELECT @errno  = 30007,
             @errmsg = 'Cannot update TsMenu because TsModule does not exist.'
      GOTO ERROR
    END
  END


  /* ERwin Builtin Trigger */
  RETURN
ERROR:
    raiserror @errno @errmsg
    rollback transaction
END

go




CREATE TRIGGER tD_TsModule ON TsModule FOR DELETE AS
/* ERwin Builtin Trigger */
/* DELETE trigger on TsModule */
BEGIN
  DECLARE  @errno   int,
           @errmsg  varchar(255)
    /* ERwin Builtin Trigger */
    /* TsModule  TsModuleRight on parent delete no action */
    /* ERWIN_RELATION:CHECKSUM="0002d148", PARENT_OWNER="", PARENT_TABLE="TsModule"
    CHILD_OWNER="", CHILD_TABLE="TsModuleRight"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_13", FK_COLUMNS="ModuleId" */
    IF EXISTS (
      SELECT * FROM deleted,TsModuleRight
      WHERE
        /*  %JoinFKPK(TsModuleRight,deleted," = "," AND") */
        TsModuleRight.ModuleId = deleted.ModuleId
    )
    BEGIN
      SELECT @errno  = 30001,
             @errmsg = 'Cannot delete TsModule because TsModuleRight exists.'
      GOTO ERROR
    END

    /* ERwin Builtin Trigger */
    /* TsModule  TsRoleRegistry on parent delete no action */
    /* ERWIN_RELATION:CHECKSUM="00000000", PARENT_OWNER="", PARENT_TABLE="TsModule"
    CHILD_OWNER="", CHILD_TABLE="TsRoleRegistry"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_15", FK_COLUMNS="ModuleId" */
    IF EXISTS (
      SELECT * FROM deleted,TsRoleRegistry
      WHERE
        /*  %JoinFKPK(TsRoleRegistry,deleted," = "," AND") */
        TsRoleRegistry.ModuleId = deleted.ModuleId
    )
    BEGIN
      SELECT @errno  = 30001,
             @errmsg = 'Cannot delete TsModule because TsRoleRegistry exists.'
      GOTO ERROR
    END

    /* ERwin Builtin Trigger */
    /* TsModule  TsMenu on parent delete no action */
    /* ERWIN_RELATION:CHECKSUM="00000000", PARENT_OWNER="", PARENT_TABLE="TsModule"
    CHILD_OWNER="", CHILD_TABLE="TsMenu"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_18", FK_COLUMNS="ModuleId" */
    IF EXISTS (
      SELECT * FROM deleted,TsMenu
      WHERE
        /*  %JoinFKPK(TsMenu,deleted," = "," AND") */
        TsMenu.ModuleId = deleted.ModuleId
    )
    BEGIN
      SELECT @errno  = 30001,
             @errmsg = 'Cannot delete TsModule because TsMenu exists.'
      GOTO ERROR
    END


    /* ERwin Builtin Trigger */
    RETURN
ERROR:
    raiserror @errno @errmsg
    rollback transaction
END

go


CREATE TRIGGER tU_TsModule ON TsModule FOR UPDATE AS
/* ERwin Builtin Trigger */
/* UPDATE trigger on TsModule */
BEGIN
  DECLARE  @NUMROWS int,
           @nullcnt int,
           @validcnt int,
           @insModuleId MDL_CD,
           @errno   int,
           @errmsg  varchar(255)

  SELECT @NUMROWS = @@rowcount
  /* ERwin Builtin Trigger */
  /* TsModule  TsModuleRight on parent update no action */
  /* ERWIN_RELATION:CHECKSUM="00031b39", PARENT_OWNER="", PARENT_TABLE="TsModule"
    CHILD_OWNER="", CHILD_TABLE="TsModuleRight"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_13", FK_COLUMNS="ModuleId" */
  IF
    /* %ParentPK(" OR",UPDATE) */
    UPDATE(ModuleId)
  BEGIN
    IF EXISTS (
      SELECT * FROM deleted,TsModuleRight
      WHERE
        /*  %JoinFKPK(TsModuleRight,deleted," = "," AND") */
        TsModuleRight.ModuleId = deleted.ModuleId
    )
    BEGIN
      SELECT @errno  = 30005,
             @errmsg = 'Cannot update TsModule because TsModuleRight exists.'
      GOTO ERROR
    END
  END

  /* ERwin Builtin Trigger */
  /* TsModule  TsRoleRegistry on parent update no action */
  /* ERWIN_RELATION:CHECKSUM="00000000", PARENT_OWNER="", PARENT_TABLE="TsModule"
    CHILD_OWNER="", CHILD_TABLE="TsRoleRegistry"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_15", FK_COLUMNS="ModuleId" */
  IF
    /* %ParentPK(" OR",UPDATE) */
    UPDATE(ModuleId)
  BEGIN
    IF EXISTS (
      SELECT * FROM deleted,TsRoleRegistry
      WHERE
        /*  %JoinFKPK(TsRoleRegistry,deleted," = "," AND") */
        TsRoleRegistry.ModuleId = deleted.ModuleId
    )
    BEGIN
      SELECT @errno  = 30005,
             @errmsg = 'Cannot update TsModule because TsRoleRegistry exists.'
      GOTO ERROR
    END
  END

  /* ERwin Builtin Trigger */
  /* TsModule  TsMenu on parent update no action */
  /* ERWIN_RELATION:CHECKSUM="00000000", PARENT_OWNER="", PARENT_TABLE="TsModule"
    CHILD_OWNER="", CHILD_TABLE="TsMenu"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_18", FK_COLUMNS="ModuleId" */
  IF
    /* %ParentPK(" OR",UPDATE) */
    UPDATE(ModuleId)
  BEGIN
    IF EXISTS (
      SELECT * FROM deleted,TsMenu
      WHERE
        /*  %JoinFKPK(TsMenu,deleted," = "," AND") */
        TsMenu.ModuleId = deleted.ModuleId
    )
    BEGIN
      SELECT @errno  = 30005,
             @errmsg = 'Cannot update TsModule because TsMenu exists.'
      GOTO ERROR
    END
  END


  /* ERwin Builtin Trigger */
  RETURN
ERROR:
    raiserror @errno @errmsg
    rollback transaction
END

go




CREATE TRIGGER tD_TsModuleRight ON TsModuleRight FOR DELETE AS
/* ERwin Builtin Trigger */
/* DELETE trigger on TsModuleRight */
BEGIN
  DECLARE  @errno   int,
           @errmsg  varchar(255)
    /* ERwin Builtin Trigger */
    /* TsModule  TsModuleRight on child delete no action */
    /* ERWIN_RELATION:CHECKSUM="00026993", PARENT_OWNER="", PARENT_TABLE="TsModule"
    CHILD_OWNER="", CHILD_TABLE="TsModuleRight"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_13", FK_COLUMNS="ModuleId" */
    IF EXISTS (SELECT * FROM deleted,TsModule
      WHERE
        /* %JoinFKPK(deleted,TsModule," = "," AND") */
        deleted.ModuleId = TsModule.ModuleId AND
        NOT EXISTS (
          SELECT * FROM TsModuleRight
          WHERE
            /* %JoinFKPK(TsModuleRight,TsModule," = "," AND") */
            TsModuleRight.ModuleId = TsModule.ModuleId
        )
    )
    BEGIN
      SELECT @errno  = 30010,
             @errmsg = 'Cannot delete last TsModuleRight because TsModule exists.'
      GOTO ERROR
    END

    /* ERwin Builtin Trigger */
    /* TsRight  TsModuleRight on child delete no action */
    /* ERWIN_RELATION:CHECKSUM="00000000", PARENT_OWNER="", PARENT_TABLE="TsRight"
    CHILD_OWNER="", CHILD_TABLE="TsModuleRight"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_14", FK_COLUMNS="RightId" */
    IF EXISTS (SELECT * FROM deleted,TsRight
      WHERE
        /* %JoinFKPK(deleted,TsRight," = "," AND") */
        deleted.RightId = TsRight.RightId AND
        NOT EXISTS (
          SELECT * FROM TsModuleRight
          WHERE
            /* %JoinFKPK(TsModuleRight,TsRight," = "," AND") */
            TsModuleRight.RightId = TsRight.RightId
        )
    )
    BEGIN
      SELECT @errno  = 30010,
             @errmsg = 'Cannot delete last TsModuleRight because TsRight exists.'
      GOTO ERROR
    END


    /* ERwin Builtin Trigger */
    RETURN
ERROR:
    raiserror @errno @errmsg
    rollback transaction
END

go


CREATE TRIGGER tU_TsModuleRight ON TsModuleRight FOR UPDATE AS
/* ERwin Builtin Trigger */
/* UPDATE trigger on TsModuleRight */
BEGIN
  DECLARE  @NUMROWS int,
           @nullcnt int,
           @validcnt int,
           @insModuleRightId ITEM_ID, 
           @insModuleId MDL_CD, 
           @insRightId ITEM_ID,
           @errno   int,
           @errmsg  varchar(255)

  SELECT @NUMROWS = @@rowcount
  /* ERwin Builtin Trigger */
  /* TsModule  TsModuleRight on child update no action */
  /* ERWIN_RELATION:CHECKSUM="00028994", PARENT_OWNER="", PARENT_TABLE="TsModule"
    CHILD_OWNER="", CHILD_TABLE="TsModuleRight"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_13", FK_COLUMNS="ModuleId" */
  IF
    /* %ChildFK(" OR",UPDATE) */
    UPDATE(ModuleId)
  BEGIN
    SELECT @nullcnt = 0
    SELECT @validcnt = count(*)
      FROM inserted,TsModule
        WHERE
          /* %JoinFKPK(inserted,TsModule) */
          inserted.ModuleId = TsModule.ModuleId
    /* %NotnullFK(inserted," IS NULL","select @nullcnt = count(*) from inserted where"," AND") */
    
    IF @validcnt + @nullcnt != @NUMROWS
    BEGIN
      SELECT @errno  = 30007,
             @errmsg = 'Cannot update TsModuleRight because TsModule does not exist.'
      GOTO ERROR
    END
  END

  /* ERwin Builtin Trigger */
  /* TsRight  TsModuleRight on child update no action */
  /* ERWIN_RELATION:CHECKSUM="00000000", PARENT_OWNER="", PARENT_TABLE="TsRight"
    CHILD_OWNER="", CHILD_TABLE="TsModuleRight"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_14", FK_COLUMNS="RightId" */
  IF
    /* %ChildFK(" OR",UPDATE) */
    UPDATE(RightId)
  BEGIN
    SELECT @nullcnt = 0
    SELECT @validcnt = count(*)
      FROM inserted,TsRight
        WHERE
          /* %JoinFKPK(inserted,TsRight) */
          inserted.RightId = TsRight.RightId
    /* %NotnullFK(inserted," IS NULL","select @nullcnt = count(*) from inserted where"," AND") */
    
    IF @validcnt + @nullcnt != @NUMROWS
    BEGIN
      SELECT @errno  = 30007,
             @errmsg = 'Cannot update TsModuleRight because TsRight does not exist.'
      GOTO ERROR
    END
  END


  /* ERwin Builtin Trigger */
  RETURN
ERROR:
    raiserror @errno @errmsg
    rollback transaction
END

go




CREATE TRIGGER tD_TsNewsCategory ON TsNewsCategory FOR DELETE AS
/* ERwin Builtin Trigger */
/* DELETE trigger on TsNewsCategory */
BEGIN
  DECLARE  @errno   int,
           @errmsg  varchar(255)
    /* ERwin Builtin Trigger */
    /* TsNewsCategory  TsArticle on parent delete no action */
    /* ERWIN_RELATION:CHECKSUM="0001d565", PARENT_OWNER="", PARENT_TABLE="TsNewsCategory"
    CHILD_OWNER="", CHILD_TABLE="TsArticle"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_1", FK_COLUMNS="CategoryId" */
    IF EXISTS (
      SELECT * FROM deleted,TsArticle
      WHERE
        /*  %JoinFKPK(TsArticle,deleted," = "," AND") */
        TsArticle.CategoryId = deleted.CategoryId
    )
    BEGIN
      SELECT @errno  = 30001,
             @errmsg = 'Cannot delete TsNewsCategory because TsArticle exists.'
      GOTO ERROR
    END

    /* ERwin Builtin Trigger */
    /* TsNewsCategory  TsFocus on parent delete no action */
    /* ERWIN_RELATION:CHECKSUM="00000000", PARENT_OWNER="", PARENT_TABLE="TsNewsCategory"
    CHILD_OWNER="", CHILD_TABLE="TsFocus"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_3", FK_COLUMNS="CategoryId" */
    IF EXISTS (
      SELECT * FROM deleted,TsFocus
      WHERE
        /*  %JoinFKPK(TsFocus,deleted," = "," AND") */
        TsFocus.CategoryId = deleted.CategoryId
    )
    BEGIN
      SELECT @errno  = 30001,
             @errmsg = 'Cannot delete TsNewsCategory because TsFocus exists.'
      GOTO ERROR
    END


    /* ERwin Builtin Trigger */
    RETURN
ERROR:
    raiserror @errno @errmsg
    rollback transaction
END

go


CREATE TRIGGER tU_TsNewsCategory ON TsNewsCategory FOR UPDATE AS
/* ERwin Builtin Trigger */
/* UPDATE trigger on TsNewsCategory */
BEGIN
  DECLARE  @NUMROWS int,
           @nullcnt int,
           @validcnt int,
           @insCategoryId ITEM_ID,
           @errno   int,
           @errmsg  varchar(255)

  SELECT @NUMROWS = @@rowcount
  /* ERwin Builtin Trigger */
  /* TsNewsCategory  TsArticle on parent update no action */
  /* ERWIN_RELATION:CHECKSUM="00021723", PARENT_OWNER="", PARENT_TABLE="TsNewsCategory"
    CHILD_OWNER="", CHILD_TABLE="TsArticle"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_1", FK_COLUMNS="CategoryId" */
  IF
    /* %ParentPK(" OR",UPDATE) */
    UPDATE(CategoryId)
  BEGIN
    IF EXISTS (
      SELECT * FROM deleted,TsArticle
      WHERE
        /*  %JoinFKPK(TsArticle,deleted," = "," AND") */
        TsArticle.CategoryId = deleted.CategoryId
    )
    BEGIN
      SELECT @errno  = 30005,
             @errmsg = 'Cannot update TsNewsCategory because TsArticle exists.'
      GOTO ERROR
    END
  END

  /* ERwin Builtin Trigger */
  /* TsNewsCategory  TsFocus on parent update no action */
  /* ERWIN_RELATION:CHECKSUM="00000000", PARENT_OWNER="", PARENT_TABLE="TsNewsCategory"
    CHILD_OWNER="", CHILD_TABLE="TsFocus"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_3", FK_COLUMNS="CategoryId" */
  IF
    /* %ParentPK(" OR",UPDATE) */
    UPDATE(CategoryId)
  BEGIN
    IF EXISTS (
      SELECT * FROM deleted,TsFocus
      WHERE
        /*  %JoinFKPK(TsFocus,deleted," = "," AND") */
        TsFocus.CategoryId = deleted.CategoryId
    )
    BEGIN
      SELECT @errno  = 30005,
             @errmsg = 'Cannot update TsNewsCategory because TsFocus exists.'
      GOTO ERROR
    END
  END


  /* ERwin Builtin Trigger */
  RETURN
ERROR:
    raiserror @errno @errmsg
    rollback transaction
END

go




CREATE TRIGGER tD_TsPermission ON TsPermission FOR DELETE AS
/* ERwin Builtin Trigger */
/* DELETE trigger on TsPermission */
BEGIN
  DECLARE  @errno   int,
           @errmsg  varchar(255)
    /* ERwin Builtin Trigger */
    /* TsRole  TsPermission on child delete no action */
    /* ERWIN_RELATION:CHECKSUM="00024dd7", PARENT_OWNER="", PARENT_TABLE="TsRole"
    CHILD_OWNER="", CHILD_TABLE="TsPermission"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_11", FK_COLUMNS="RoleId" */
    IF EXISTS (SELECT * FROM deleted,TsRole
      WHERE
        /* %JoinFKPK(deleted,TsRole," = "," AND") */
        deleted.RoleId = TsRole.RoleId AND
        NOT EXISTS (
          SELECT * FROM TsPermission
          WHERE
            /* %JoinFKPK(TsPermission,TsRole," = "," AND") */
            TsPermission.RoleId = TsRole.RoleId
        )
    )
    BEGIN
      SELECT @errno  = 30010,
             @errmsg = 'Cannot delete last TsPermission because TsRole exists.'
      GOTO ERROR
    END

    /* ERwin Builtin Trigger */
    /* TsUser  TsPermission on child delete no action */
    /* ERWIN_RELATION:CHECKSUM="00000000", PARENT_OWNER="", PARENT_TABLE="TsUser"
    CHILD_OWNER="", CHILD_TABLE="TsPermission"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_12", FK_COLUMNS="UserId" */
    IF EXISTS (SELECT * FROM deleted,TsUser
      WHERE
        /* %JoinFKPK(deleted,TsUser," = "," AND") */
        deleted.UserId = TsUser.UserId AND
        NOT EXISTS (
          SELECT * FROM TsPermission
          WHERE
            /* %JoinFKPK(TsPermission,TsUser," = "," AND") */
            TsPermission.UserId = TsUser.UserId
        )
    )
    BEGIN
      SELECT @errno  = 30010,
             @errmsg = 'Cannot delete last TsPermission because TsUser exists.'
      GOTO ERROR
    END


    /* ERwin Builtin Trigger */
    RETURN
ERROR:
    raiserror @errno @errmsg
    rollback transaction
END

go


CREATE TRIGGER tU_TsPermission ON TsPermission FOR UPDATE AS
/* ERwin Builtin Trigger */
/* UPDATE trigger on TsPermission */
BEGIN
  DECLARE  @NUMROWS int,
           @nullcnt int,
           @validcnt int,
           @insPermissionId ITEM_ID, 
           @insUserId USER_ID, 
           @insRoleId ITEM_ID,
           @errno   int,
           @errmsg  varchar(255)

  SELECT @NUMROWS = @@rowcount
  /* ERwin Builtin Trigger */
  /* TsRole  TsPermission on child update no action */
  /* ERWIN_RELATION:CHECKSUM="00028bc1", PARENT_OWNER="", PARENT_TABLE="TsRole"
    CHILD_OWNER="", CHILD_TABLE="TsPermission"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_11", FK_COLUMNS="RoleId" */
  IF
    /* %ChildFK(" OR",UPDATE) */
    UPDATE(RoleId)
  BEGIN
    SELECT @nullcnt = 0
    SELECT @validcnt = count(*)
      FROM inserted,TsRole
        WHERE
          /* %JoinFKPK(inserted,TsRole) */
          inserted.RoleId = TsRole.RoleId
    /* %NotnullFK(inserted," IS NULL","select @nullcnt = count(*) from inserted where"," AND") */
    
    IF @validcnt + @nullcnt != @NUMROWS
    BEGIN
      SELECT @errno  = 30007,
             @errmsg = 'Cannot update TsPermission because TsRole does not exist.'
      GOTO ERROR
    END
  END

  /* ERwin Builtin Trigger */
  /* TsUser  TsPermission on child update no action */
  /* ERWIN_RELATION:CHECKSUM="00000000", PARENT_OWNER="", PARENT_TABLE="TsUser"
    CHILD_OWNER="", CHILD_TABLE="TsPermission"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_12", FK_COLUMNS="UserId" */
  IF
    /* %ChildFK(" OR",UPDATE) */
    UPDATE(UserId)
  BEGIN
    SELECT @nullcnt = 0
    SELECT @validcnt = count(*)
      FROM inserted,TsUser
        WHERE
          /* %JoinFKPK(inserted,TsUser) */
          inserted.UserId = TsUser.UserId
    /* %NotnullFK(inserted," IS NULL","select @nullcnt = count(*) from inserted where"," AND") */
    
    IF @validcnt + @nullcnt != @NUMROWS
    BEGIN
      SELECT @errno  = 30007,
             @errmsg = 'Cannot update TsPermission because TsUser does not exist.'
      GOTO ERROR
    END
  END


  /* ERwin Builtin Trigger */
  RETURN
ERROR:
    raiserror @errno @errmsg
    rollback transaction
END

go




CREATE TRIGGER tD_TsPhone ON TsPhone FOR DELETE AS
/* ERwin Builtin Trigger */
/* DELETE trigger on TsPhone */
BEGIN
  DECLARE  @errno   int,
           @errmsg  varchar(255)
    /* ERwin Builtin Trigger */
    /* TsUser  TsPhone on child delete no action */
    /* ERWIN_RELATION:CHECKSUM="000118ff", PARENT_OWNER="", PARENT_TABLE="TsUser"
    CHILD_OWNER="", CHILD_TABLE="TsPhone"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_26", FK_COLUMNS="UserId" */
    IF EXISTS (SELECT * FROM deleted,TsUser
      WHERE
        /* %JoinFKPK(deleted,TsUser," = "," AND") */
        deleted.UserId = TsUser.UserId AND
        NOT EXISTS (
          SELECT * FROM TsPhone
          WHERE
            /* %JoinFKPK(TsPhone,TsUser," = "," AND") */
            TsPhone.UserId = TsUser.UserId
        )
    )
    BEGIN
      SELECT @errno  = 30010,
             @errmsg = 'Cannot delete last TsPhone because TsUser exists.'
      GOTO ERROR
    END


    /* ERwin Builtin Trigger */
    RETURN
ERROR:
    raiserror @errno @errmsg
    rollback transaction
END

go


CREATE TRIGGER tU_TsPhone ON TsPhone FOR UPDATE AS
/* ERwin Builtin Trigger */
/* UPDATE trigger on TsPhone */
BEGIN
  DECLARE  @NUMROWS int,
           @nullcnt int,
           @validcnt int,
           @insPhoneId ITEM_ID, 
           @insUserId USER_ID,
           @errno   int,
           @errmsg  varchar(255)

  SELECT @NUMROWS = @@rowcount
  /* ERwin Builtin Trigger */
  /* TsUser  TsPhone on child update no action */
  /* ERWIN_RELATION:CHECKSUM="00014a23", PARENT_OWNER="", PARENT_TABLE="TsUser"
    CHILD_OWNER="", CHILD_TABLE="TsPhone"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_26", FK_COLUMNS="UserId" */
  IF
    /* %ChildFK(" OR",UPDATE) */
    UPDATE(UserId)
  BEGIN
    SELECT @nullcnt = 0
    SELECT @validcnt = count(*)
      FROM inserted,TsUser
        WHERE
          /* %JoinFKPK(inserted,TsUser) */
          inserted.UserId = TsUser.UserId
    /* %NotnullFK(inserted," IS NULL","select @nullcnt = count(*) from inserted where"," AND") */
    
    IF @validcnt + @nullcnt != @NUMROWS
    BEGIN
      SELECT @errno  = 30007,
             @errmsg = 'Cannot update TsPhone because TsUser does not exist.'
      GOTO ERROR
    END
  END


  /* ERwin Builtin Trigger */
  RETURN
ERROR:
    raiserror @errno @errmsg
    rollback transaction
END

go




CREATE TRIGGER tD_TsRight ON TsRight FOR DELETE AS
/* ERwin Builtin Trigger */
/* DELETE trigger on TsRight */
BEGIN
  DECLARE  @errno   int,
           @errmsg  varchar(255)
    /* ERwin Builtin Trigger */
    /* TsRight  TsModuleRight on parent delete no action */
    /* ERWIN_RELATION:CHECKSUM="0000f465", PARENT_OWNER="", PARENT_TABLE="TsRight"
    CHILD_OWNER="", CHILD_TABLE="TsModuleRight"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_14", FK_COLUMNS="RightId" */
    IF EXISTS (
      SELECT * FROM deleted,TsModuleRight
      WHERE
        /*  %JoinFKPK(TsModuleRight,deleted," = "," AND") */
        TsModuleRight.RightId = deleted.RightId
    )
    BEGIN
      SELECT @errno  = 30001,
             @errmsg = 'Cannot delete TsRight because TsModuleRight exists.'
      GOTO ERROR
    END


    /* ERwin Builtin Trigger */
    RETURN
ERROR:
    raiserror @errno @errmsg
    rollback transaction
END

go


CREATE TRIGGER tU_TsRight ON TsRight FOR UPDATE AS
/* ERwin Builtin Trigger */
/* UPDATE trigger on TsRight */
BEGIN
  DECLARE  @NUMROWS int,
           @nullcnt int,
           @validcnt int,
           @insRightId ITEM_ID,
           @errno   int,
           @errmsg  varchar(255)

  SELECT @NUMROWS = @@rowcount
  /* ERwin Builtin Trigger */
  /* TsRight  TsModuleRight on parent update no action */
  /* ERWIN_RELATION:CHECKSUM="000113d1", PARENT_OWNER="", PARENT_TABLE="TsRight"
    CHILD_OWNER="", CHILD_TABLE="TsModuleRight"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_14", FK_COLUMNS="RightId" */
  IF
    /* %ParentPK(" OR",UPDATE) */
    UPDATE(RightId)
  BEGIN
    IF EXISTS (
      SELECT * FROM deleted,TsModuleRight
      WHERE
        /*  %JoinFKPK(TsModuleRight,deleted," = "," AND") */
        TsModuleRight.RightId = deleted.RightId
    )
    BEGIN
      SELECT @errno  = 30005,
             @errmsg = 'Cannot update TsRight because TsModuleRight exists.'
      GOTO ERROR
    END
  END


  /* ERwin Builtin Trigger */
  RETURN
ERROR:
    raiserror @errno @errmsg
    rollback transaction
END

go




CREATE TRIGGER tD_TsRole ON TsRole FOR DELETE AS
/* ERwin Builtin Trigger */
/* DELETE trigger on TsRole */
BEGIN
  DECLARE  @errno   int,
           @errmsg  varchar(255)
    /* ERwin Builtin Trigger */
    /* TsRole  TsPermission on parent delete no action */
    /* ERWIN_RELATION:CHECKSUM="0001dedc", PARENT_OWNER="", PARENT_TABLE="TsRole"
    CHILD_OWNER="", CHILD_TABLE="TsPermission"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_11", FK_COLUMNS="RoleId" */
    IF EXISTS (
      SELECT * FROM deleted,TsPermission
      WHERE
        /*  %JoinFKPK(TsPermission,deleted," = "," AND") */
        TsPermission.RoleId = deleted.RoleId
    )
    BEGIN
      SELECT @errno  = 30001,
             @errmsg = 'Cannot delete TsRole because TsPermission exists.'
      GOTO ERROR
    END

    /* ERwin Builtin Trigger */
    /* TsRole  TsRoleRegistry on parent delete no action */
    /* ERWIN_RELATION:CHECKSUM="00000000", PARENT_OWNER="", PARENT_TABLE="TsRole"
    CHILD_OWNER="", CHILD_TABLE="TsRoleRegistry"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_16", FK_COLUMNS="RoleId" */
    IF EXISTS (
      SELECT * FROM deleted,TsRoleRegistry
      WHERE
        /*  %JoinFKPK(TsRoleRegistry,deleted," = "," AND") */
        TsRoleRegistry.RoleId = deleted.RoleId
    )
    BEGIN
      SELECT @errno  = 30001,
             @errmsg = 'Cannot delete TsRole because TsRoleRegistry exists.'
      GOTO ERROR
    END


    /* ERwin Builtin Trigger */
    RETURN
ERROR:
    raiserror @errno @errmsg
    rollback transaction
END

go


CREATE TRIGGER tU_TsRole ON TsRole FOR UPDATE AS
/* ERwin Builtin Trigger */
/* UPDATE trigger on TsRole */
BEGIN
  DECLARE  @NUMROWS int,
           @nullcnt int,
           @validcnt int,
           @insRoleId ITEM_ID,
           @errno   int,
           @errmsg  varchar(255)

  SELECT @NUMROWS = @@rowcount
  /* ERwin Builtin Trigger */
  /* TsRole  TsPermission on parent update no action */
  /* ERWIN_RELATION:CHECKSUM="00021425", PARENT_OWNER="", PARENT_TABLE="TsRole"
    CHILD_OWNER="", CHILD_TABLE="TsPermission"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_11", FK_COLUMNS="RoleId" */
  IF
    /* %ParentPK(" OR",UPDATE) */
    UPDATE(RoleId)
  BEGIN
    IF EXISTS (
      SELECT * FROM deleted,TsPermission
      WHERE
        /*  %JoinFKPK(TsPermission,deleted," = "," AND") */
        TsPermission.RoleId = deleted.RoleId
    )
    BEGIN
      SELECT @errno  = 30005,
             @errmsg = 'Cannot update TsRole because TsPermission exists.'
      GOTO ERROR
    END
  END

  /* ERwin Builtin Trigger */
  /* TsRole  TsRoleRegistry on parent update no action */
  /* ERWIN_RELATION:CHECKSUM="00000000", PARENT_OWNER="", PARENT_TABLE="TsRole"
    CHILD_OWNER="", CHILD_TABLE="TsRoleRegistry"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_16", FK_COLUMNS="RoleId" */
  IF
    /* %ParentPK(" OR",UPDATE) */
    UPDATE(RoleId)
  BEGIN
    IF EXISTS (
      SELECT * FROM deleted,TsRoleRegistry
      WHERE
        /*  %JoinFKPK(TsRoleRegistry,deleted," = "," AND") */
        TsRoleRegistry.RoleId = deleted.RoleId
    )
    BEGIN
      SELECT @errno  = 30005,
             @errmsg = 'Cannot update TsRole because TsRoleRegistry exists.'
      GOTO ERROR
    END
  END


  /* ERwin Builtin Trigger */
  RETURN
ERROR:
    raiserror @errno @errmsg
    rollback transaction
END

go




CREATE TRIGGER tD_TsRoleRegistry ON TsRoleRegistry FOR DELETE AS
/* ERwin Builtin Trigger */
/* DELETE trigger on TsRoleRegistry */
BEGIN
  DECLARE  @errno   int,
           @errmsg  varchar(255)
    /* ERwin Builtin Trigger */
    /* TsModule  TsRoleRegistry on child delete no action */
    /* ERWIN_RELATION:CHECKSUM="000253f6", PARENT_OWNER="", PARENT_TABLE="TsModule"
    CHILD_OWNER="", CHILD_TABLE="TsRoleRegistry"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_15", FK_COLUMNS="ModuleId" */
    IF EXISTS (SELECT * FROM deleted,TsModule
      WHERE
        /* %JoinFKPK(deleted,TsModule," = "," AND") */
        deleted.ModuleId = TsModule.ModuleId AND
        NOT EXISTS (
          SELECT * FROM TsRoleRegistry
          WHERE
            /* %JoinFKPK(TsRoleRegistry,TsModule," = "," AND") */
            TsRoleRegistry.ModuleId = TsModule.ModuleId
        )
    )
    BEGIN
      SELECT @errno  = 30010,
             @errmsg = 'Cannot delete last TsRoleRegistry because TsModule exists.'
      GOTO ERROR
    END

    /* ERwin Builtin Trigger */
    /* TsRole  TsRoleRegistry on child delete no action */
    /* ERWIN_RELATION:CHECKSUM="00000000", PARENT_OWNER="", PARENT_TABLE="TsRole"
    CHILD_OWNER="", CHILD_TABLE="TsRoleRegistry"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_16", FK_COLUMNS="RoleId" */
    IF EXISTS (SELECT * FROM deleted,TsRole
      WHERE
        /* %JoinFKPK(deleted,TsRole," = "," AND") */
        deleted.RoleId = TsRole.RoleId AND
        NOT EXISTS (
          SELECT * FROM TsRoleRegistry
          WHERE
            /* %JoinFKPK(TsRoleRegistry,TsRole," = "," AND") */
            TsRoleRegistry.RoleId = TsRole.RoleId
        )
    )
    BEGIN
      SELECT @errno  = 30010,
             @errmsg = 'Cannot delete last TsRoleRegistry because TsRole exists.'
      GOTO ERROR
    END


    /* ERwin Builtin Trigger */
    RETURN
ERROR:
    raiserror @errno @errmsg
    rollback transaction
END

go


CREATE TRIGGER tU_TsRoleRegistry ON TsRoleRegistry FOR UPDATE AS
/* ERwin Builtin Trigger */
/* UPDATE trigger on TsRoleRegistry */
BEGIN
  DECLARE  @NUMROWS int,
           @nullcnt int,
           @validcnt int,
           @insRoleRegistryId ITEM_ID, 
           @insModuleId MDL_CD, 
           @insRoleId ITEM_ID,
           @errno   int,
           @errmsg  varchar(255)

  SELECT @NUMROWS = @@rowcount
  /* ERwin Builtin Trigger */
  /* TsModule  TsRoleRegistry on child update no action */
  /* ERWIN_RELATION:CHECKSUM="0002a306", PARENT_OWNER="", PARENT_TABLE="TsModule"
    CHILD_OWNER="", CHILD_TABLE="TsRoleRegistry"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_15", FK_COLUMNS="ModuleId" */
  IF
    /* %ChildFK(" OR",UPDATE) */
    UPDATE(ModuleId)
  BEGIN
    SELECT @nullcnt = 0
    SELECT @validcnt = count(*)
      FROM inserted,TsModule
        WHERE
          /* %JoinFKPK(inserted,TsModule) */
          inserted.ModuleId = TsModule.ModuleId
    /* %NotnullFK(inserted," IS NULL","select @nullcnt = count(*) from inserted where"," AND") */
    
    IF @validcnt + @nullcnt != @NUMROWS
    BEGIN
      SELECT @errno  = 30007,
             @errmsg = 'Cannot update TsRoleRegistry because TsModule does not exist.'
      GOTO ERROR
    END
  END

  /* ERwin Builtin Trigger */
  /* TsRole  TsRoleRegistry on child update no action */
  /* ERWIN_RELATION:CHECKSUM="00000000", PARENT_OWNER="", PARENT_TABLE="TsRole"
    CHILD_OWNER="", CHILD_TABLE="TsRoleRegistry"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_16", FK_COLUMNS="RoleId" */
  IF
    /* %ChildFK(" OR",UPDATE) */
    UPDATE(RoleId)
  BEGIN
    SELECT @nullcnt = 0
    SELECT @validcnt = count(*)
      FROM inserted,TsRole
        WHERE
          /* %JoinFKPK(inserted,TsRole) */
          inserted.RoleId = TsRole.RoleId
    /* %NotnullFK(inserted," IS NULL","select @nullcnt = count(*) from inserted where"," AND") */
    
    IF @validcnt + @nullcnt != @NUMROWS
    BEGIN
      SELECT @errno  = 30007,
             @errmsg = 'Cannot update TsRoleRegistry because TsRole does not exist.'
      GOTO ERROR
    END
  END


  /* ERwin Builtin Trigger */
  RETURN
ERROR:
    raiserror @errno @errmsg
    rollback transaction
END

go




CREATE TRIGGER tD_TsUser ON TsUser FOR DELETE AS
/* ERwin Builtin Trigger */
/* DELETE trigger on TsUser */
BEGIN
  DECLARE  @errno   int,
           @errmsg  varchar(255)
    /* ERwin Builtin Trigger */
    /* TsUser  TsPermission on parent delete no action */
    /* ERWIN_RELATION:CHECKSUM="0002add4", PARENT_OWNER="", PARENT_TABLE="TsUser"
    CHILD_OWNER="", CHILD_TABLE="TsPermission"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_12", FK_COLUMNS="UserId" */
    IF EXISTS (
      SELECT * FROM deleted,TsPermission
      WHERE
        /*  %JoinFKPK(TsPermission,deleted," = "," AND") */
        TsPermission.UserId = deleted.UserId
    )
    BEGIN
      SELECT @errno  = 30001,
             @errmsg = 'Cannot delete TsUser because TsPermission exists.'
      GOTO ERROR
    END

    /* ERwin Builtin Trigger */
    /* TsUser  TsAddress on parent delete no action */
    /* ERWIN_RELATION:CHECKSUM="00000000", PARENT_OWNER="", PARENT_TABLE="TsUser"
    CHILD_OWNER="", CHILD_TABLE="TsAddress"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_25", FK_COLUMNS="UserId" */
    IF EXISTS (
      SELECT * FROM deleted,TsAddress
      WHERE
        /*  %JoinFKPK(TsAddress,deleted," = "," AND") */
        TsAddress.UserId = deleted.UserId
    )
    BEGIN
      SELECT @errno  = 30001,
             @errmsg = 'Cannot delete TsUser because TsAddress exists.'
      GOTO ERROR
    END

    /* ERwin Builtin Trigger */
    /* TsUser  TsPhone on parent delete no action */
    /* ERWIN_RELATION:CHECKSUM="00000000", PARENT_OWNER="", PARENT_TABLE="TsUser"
    CHILD_OWNER="", CHILD_TABLE="TsPhone"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_26", FK_COLUMNS="UserId" */
    IF EXISTS (
      SELECT * FROM deleted,TsPhone
      WHERE
        /*  %JoinFKPK(TsPhone,deleted," = "," AND") */
        TsPhone.UserId = deleted.UserId
    )
    BEGIN
      SELECT @errno  = 30001,
             @errmsg = 'Cannot delete TsUser because TsPhone exists.'
      GOTO ERROR
    END


    /* ERwin Builtin Trigger */
    RETURN
ERROR:
    raiserror @errno @errmsg
    rollback transaction
END

go


CREATE TRIGGER tU_TsUser ON TsUser FOR UPDATE AS
/* ERwin Builtin Trigger */
/* UPDATE trigger on TsUser */
BEGIN
  DECLARE  @NUMROWS int,
           @nullcnt int,
           @validcnt int,
           @insUserId USER_ID,
           @errno   int,
           @errmsg  varchar(255)

  SELECT @NUMROWS = @@rowcount
  /* ERwin Builtin Trigger */
  /* TsUser  TsPermission on parent update no action */
  /* ERWIN_RELATION:CHECKSUM="0002f8b8", PARENT_OWNER="", PARENT_TABLE="TsUser"
    CHILD_OWNER="", CHILD_TABLE="TsPermission"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_12", FK_COLUMNS="UserId" */
  IF
    /* %ParentPK(" OR",UPDATE) */
    UPDATE(UserId)
  BEGIN
    IF EXISTS (
      SELECT * FROM deleted,TsPermission
      WHERE
        /*  %JoinFKPK(TsPermission,deleted," = "," AND") */
        TsPermission.UserId = deleted.UserId
    )
    BEGIN
      SELECT @errno  = 30005,
             @errmsg = 'Cannot update TsUser because TsPermission exists.'
      GOTO ERROR
    END
  END

  /* ERwin Builtin Trigger */
  /* TsUser  TsAddress on parent update no action */
  /* ERWIN_RELATION:CHECKSUM="00000000", PARENT_OWNER="", PARENT_TABLE="TsUser"
    CHILD_OWNER="", CHILD_TABLE="TsAddress"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_25", FK_COLUMNS="UserId" */
  IF
    /* %ParentPK(" OR",UPDATE) */
    UPDATE(UserId)
  BEGIN
    IF EXISTS (
      SELECT * FROM deleted,TsAddress
      WHERE
        /*  %JoinFKPK(TsAddress,deleted," = "," AND") */
        TsAddress.UserId = deleted.UserId
    )
    BEGIN
      SELECT @errno  = 30005,
             @errmsg = 'Cannot update TsUser because TsAddress exists.'
      GOTO ERROR
    END
  END

  /* ERwin Builtin Trigger */
  /* TsUser  TsPhone on parent update no action */
  /* ERWIN_RELATION:CHECKSUM="00000000", PARENT_OWNER="", PARENT_TABLE="TsUser"
    CHILD_OWNER="", CHILD_TABLE="TsPhone"
    P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", 
    FK_CONSTRAINT="R_26", FK_COLUMNS="UserId" */
  IF
    /* %ParentPK(" OR",UPDATE) */
    UPDATE(UserId)
  BEGIN
    IF EXISTS (
      SELECT * FROM deleted,TsPhone
      WHERE
        /*  %JoinFKPK(TsPhone,deleted," = "," AND") */
        TsPhone.UserId = deleted.UserId
    )
    BEGIN
      SELECT @errno  = 30005,
             @errmsg = 'Cannot update TsUser because TsPhone exists.'
      GOTO ERROR
    END
  END


  /* ERwin Builtin Trigger */
  RETURN
ERROR:
    raiserror @errno @errmsg
    rollback transaction
END

go


